<?php
/**
 * variables of page: pollItemId
 */
$form = $this->beginWidget('CActiveForm', array(
    "id" => "addPollItemForm",
    'enableAjaxValidation' => FALSE,
    'htmlOptions' => array('enctype' => 'multipart/form-data'),
        ));
$url = $control->getAjaxLinkUrl('savePollItem', NULL, array(
    'pollItemId' => $pollItemId));
$updateId = $control->getAjaxContainerId();
?>
<script type="text/javascript">
// this script for collecting the form data and pass to the controller action and doing the on success validations
    function savePollItem<?php echo $pollItemId; ?>() {

        var formData = new FormData($("#addPollItemForm")[0]);
        $.ajax({
            url: '<?php echo $url; ?>',
            type: 'POST',
            data: formData,
            //datatype: 'json',
            // async: false,
            beforeSend: function() {
                $("#ajax-loaderdiv").show();
            },
            success: function(html) {
                // on success do some validation or refresh the content div to display the uploaded images 
                jQuery("#<?php echo $updateId; ?>").html(html);
            },
            complete: function() {
                $("#ajax-loaderdiv").hide();
            },
            error: function(data) {
                alert("There may a error on uploading. Try again later");
            },
            cache: false,
            contentType: false,
            processData: false
        });

        return false;
    }
</script>
<legend>
    <h4>
        <?php echo $control->getAjaxLink(array('name' => '投票列表', 'id' => 'showPollList')); ?>\       
        <?php echo '发布投票'; ?>
    </h4>
</legend>

<?php
if (!isset($model))
    $model = new BDynamicFormModel ();
$pollQuestionField = 'media_question_poll_item';
$pollOption1Field = 'media_option1_poll_item';
$pollOption2Field = 'media_option2_poll_item';

$model->addField($pollQuestionField);
$model->addField($pollOption1Field);
$model->addField($pollOption2Field);

$optionNameField = 'media_name_pollItem_option';
$optionField = 'media_option_pollItem_option';
$model->addField($optionNameField);
$model->addField($optionField);

$attributeNameField = 'media_name_pollItem_attribute';
$attributeAliasField = 'media_alias_pollItem_attribute';
$attributeTypeField = 'media_type_pollItem_attribute';
$model->addField($attributeNameField);
$model->addField($attributeAliasField);
$model->addField($attributeTypeField);

$pollAttributeView = array();
$optionView = array();

if ($pollItemId > 0) {
    $pollItemTable = new BWWePollSheet('wePollItem');
    $pollItem = $pollItem->getListById($pollItemId);

    $pollAttributeTable = new BWWePollSheet('pollItemAttribute', $pollItem);
    $pollAttributeView = $pollAttributeTable->getView();

    $optionTable = new BWWePollSheet('pollItemOption', $pollItem);
    $optionView = $optionTable->getView();

    $pollQuestionItem = $pollItem->getItem('question');
    if ($pollQuestionItem instanceof BTreeItem) {
        $pollQuestion = $pollQuestionItem->getValue();
        $model->$pollQuestionField = $pollQuestion;
    }
    $pollOption1Item = $poll->getItem('option1');
    if ($pollOption1Item instanceof BTreeItem) {
        $option1 = $pollOption1FieldItem->getValue();
        $model->$pollOption1Field = $option1;
    }
    $pollOption2Item = $poll->getItem('option2');
    if ($pollOption2Item instanceof BTreeItem) {
        $option2 = $pollOption2Item->getValue();
        $model->$pollOption2Field = $option2;
    }
}
?>
<?php
$app = Blyn::app()->getActiveApp();
?>


<link rel="stylesheet" type="text/css"
      href="<?php echo Yii::app()->request->baseUrl; ?>/css/bootstrap-datetimepicker.min.css" />
<script  type="text/javascript"
         src="<?php echo Yii::app()->request->baseUrl; ?>/js/bootstrap-datetimepicker.min.js"/>
<script type="text/javascript">
    $(function() {
        $('#datetimepicker1').datetimepicker({
            language: 'pt-BR'
        });
    });</script>
<div class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">选择项内容</span>
        <?php
        echo CHtml::activeTextField($model, $pollOptionField, array('placeholder' => 'alias' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
        echo $control->getAjaxLink(array('name' => '添加选择项', 'id' => 'addPollOption'), array(
            'cooker' => 'BWeEventWePoll\addPollItemOption',
            'htmlOptions' => array('class' => 'btn'),
            'pollItemId' => $pollItemId,
            'attributeId' => 0
        ));
        ?>
    </div>
</div>
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>
    <thead>
        <tr>
            <th><?php echo '属性'; ?></th>
            <th><?php echo '内容'; ?></th>
            <th><?php echo '操作'; ?></th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>
                <?php echo '投票问题'; ?>
            </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $pollNameField, array('placeholder' => 'poll name' . '...', 'required' => true));
                ?>
                <!--                    &nbsp;&nbsp;&nbsp;&nbsp;<a id="CreateNewType_clink" role="button" data-toggle="modal" class="btn disabled" style="font-size: 12px;margin-top: -12px;">创建新类型</a>-->
            </td>
            <td></td>
        </tr>
        <tr><td> <?php echo '选择项1'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $pollOption1Field, array('placeholder' => 'poll option1' . '...', 'required' => true));
                ?>
            </td>
            <td></td>
        </tr>   
        <tr><td> <?php echo '选择项2'; ?> </td>
            <td>
                <?php
                echo CHtml::activeTextField($model, $pollOption2Field, array('placeholder' => 'poll id' . '...', 'required' => true));
                ?>
            </td>
            <td></td>
        </tr>

        <?php
        if ($pollId > 0) {
            $appCommon = new BAppCommon();
            foreach ($optionView as $pollOption) {
                $optionId = $pollOption->getId();
                ?>
                <tr>
                    <?php
                    $nameItem = $pollOption->getItem('name');
                    $typeItem = $pollOption->getItem('option');
                    $name = '';
                    $option = '';
                    $optionValue = '';
                    if ($nameItem instanceof BTreeItem) {
                        $name = $nameItem->getValue();
                        $optionValueField = 'media_' . $name . '_poll_item';
                        $optionValueItem = $pollItem->getItem($name);
                        if ($optionValueItem instanceof BTreeItem) {
                            $optionValue = $optionValueItem->getValue();
                            $model->$optionValueField = $optionValue;
                        } else
                            $model->$optionValueField = '';
                    }
                    ?>
                    <td> <?php echo $name ?>  </td>   
                    <td> <?php
                        $modelData = array();
                        $modelData['model'] = $model;
                        $modelData['field'] = $attributeField;
                        echo $appCommon->showActiveFieldByDataType($type, $modelData);
                        ?>  
                    </td>
                    <td>
                        <?php
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deletePollAttribute'), array(
                            'cooker' => 'BWeEventWePollCooker\deletePollAttribute',
                            'htmlOptions' => array('class' => 'btn'),
                            'pollId' => $pollId,
                            'attributeId' => $attributeId
                        ));
                        ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>

<legend>添加属性</legend>
<div class="well-small row-fluid">
    <div class="input-prepend span3">
        <span class="add-on">属性名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeNameField, array('placeholder' => 'attribute' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">别名</span>
        <?php
        echo CHtml::activeTextField($model, $attributeAliasField, array('placeholder' => 'alias' . '...', 'required' => true, 'class' => 'span9'));
        ?>
    </div>
    <div class="input-prepend span3">
        <span class="add-on">类型</span>
        <?php
        $bAppCommon = new BAppCommon();
        $typeList = $bAppCommon->getDataTypeList();
        echo CHtml::activeDropDownList($model, $attributeTypeField, $typeList, array('required' => true, 'class' => 'span8'));
        ?>
    </div>
    <div class="input-append span2">
        <?php
        echo $control->getAjaxLink(array('name' => '添加属性', 'id' => 'addPollItemAttribute'), array(
            'cooker' => 'BWeEventWePoll\addPollItemAttribute',
            'htmlOptions' => array('class' => 'btn'),
            'pollItemId' => $pollItemId,
            'attributeId' => 0
        ));
        ?>
    </div>
</div>
<table class="table table-hover">
    <colgroup>
        <col class="span2">
        <col class="span4">
        <col class="span4">
    </colgroup>    
    <tbody>     
        <?php
        if ($pollId > 0) {
            $appCommon = new BAppCommon();
            foreach ($pollAttributeView as $pollAttribute) {
                $attributeId = $pollAttribute->getId();
                ?>
                <tr>
                    <?php
                    $nameItem = $pollAttribute->getItem('name');
                    $typeItem = $pollAttribute->getItem('type');
                    $aliasItem = $pollAttribute->getItem('alias');
                    $name = '';
                    $type = '';
                    $alias = '';
                    $attributeValue = '';
                    if ($nameItem instanceof BTreeItem) {
                        $name = $nameItem->getValue();
                        $attributeField = 'media_' . $name . '_poll_item';
                        $attributeItem = $poll->getItem($name);
                        if ($attributeItem instanceof BTreeItem) {
                            $attributeValue = $attributeItem->getValue();
                            $model->$attributeField = $attributeValue;
                        } else
                            $model->$attributeField = '';
                    }
                    if ($typeItem instanceof BTreeItem) {
                        $type = $typeItem->getValue();
                        //get typeData from define in attribute
                        $typeData = $app->getModelDataByDataType($typeItem);
                    }
                    if ($aliasItem instanceof BTreeItem) {
                        $alias = $aliasItem->getValue();
                    }
                    if ($alias == '')
                        $alias = $name;
                    ?>
                    <td> <?php echo $alias; ?>  </td>   
                    <td> <?php
                        $modelData = array();
                        $modelData['model'] = $model;
                        $modelData['field'] = $attributeField;
                        $modelData['data'] = $typeData;
                        if (isset($form))
                            $modelData['form'] = $form;
                        echo $appCommon->showActiveFieldByDataType($type, $modelData);
                        ?>  
                    </td>
                    <td>
                        <?php
                        echo $control->getAjaxLink(array('name' => '删除', 'id' => 'deletePollAttribute'), array(
                            'htmlOptions' => array('class' => 'btn'),
                            'pollId' => $pollId,
                            'attributeId' => $attributeId
                        ));
                        ?>
                    </td>
                </tr>
                <?php
            }
        }
        ?>
    </tbody>
</table>



<?php
$this->endWidget();
//echo CHtml::endForm();
?>
<button class="btn" onclick="javascript:savePoll<?php echo $pollId; ?>()"><?php echo '保存投票' ?></button>


